package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import org.osmf.containers.MediaContainer;
	import org.osmf.elements.SerialElement;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.layout.ScaleMode;
	import org.osmf.media.DefaultMediaFactory;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaPlayer;
	import org.osmf.net.StreamType;
	import org.osmf.net.StreamingURLResource;
	
	//Sets the size of the SWF
	
	[SWF(width="800", height="600")]
	public class WT05_SerialCompositions extends MovieClip
	{
		import org.osmf.elements.VideoElement;
		import org.osmf.media.URLResource;
		import org.osmf.utils.URL;
		
		////////////////////////////////////////////////////
		//DECLARATIONS
		////////////////////////////////////////////////////
		
		//URI of the media
		public static const PROGRESSIVE_PATH:String = "http://mediapm.edgesuite.net/strobe/content/test/AFaerysTale_sylviaApostol_640_500_short.flv";
		public static const STREAMING_PATH:String = "rtmp://cp67126.edgefcs.net/ondemand/mediapm/osmf/content/test/akamai_10_year_f8_512K";
		public static const STREAMING_MP4_PATH:String = "rtmp://cp67126.edgefcs.net/ondemand/mp4:mediapm/ovp/content/demo/video/elephants_dream/elephants_dream_768x428_24.0fps_408kbps.mp4";
		public static const LOCAL_SWF:String = "assets/swf_banner.swf";
		public static const DYNAMIC_STREAMING:String = "http://mediapm.edgesuite.net/osmf/content/test/manifest-files/dynamic_Streaming.f4m";
		public static const LOGO_VID:String = "http://mediapm.edgesuite.net/osmf/content/test/logo_animated.flv";
		//http://s7d1.scene7.com/s7/video/flash_video.jsp?movie=Videos/adverts/2010/vp067/very%2Dxmas%2Dfinal.flv
		public static const QUIZ_SWF:String = "assets/quiz.swf";
		
		public var player:MediaPlayer;
		public var container:MediaContainer;
		public var mediaFactory:DefaultMediaFactory;
		
		////////////////////////////////////////////////////
		//CONSTRUCTOR
		////////////////////////////////////////////////////
		
		public function WT05_SerialCompositions()
		{
			
			initPlayer();
		}
		
		
		
		
		protected function initPlayer():void
		{
			// Create a mediafactory instance
			mediaFactory = new DefaultMediaFactory();
			
			//Marker 1: create a video element for the pre-roll video - use the newly added LOGO_VID const
			var preRoll:MediaElement = mediaFactory.createMediaElement( new URLResource( LOGO_VID ) );
			
			//Marker 2: Create a StreamingURLResource to create a sub clip of the streaming resource - play from 0-20 sec
			var resourceStart:StreamingURLResource = new StreamingURLResource( DYNAMIC_STREAMING, StreamType.RECORDED, 0, 20 );
			
			//Marker 3: Create the media element from the streaming resource sub clip
			var element:MediaElement = mediaFactory.createMediaElement( resourceStart );
			
			//Marker 4: Create another post roll clip that is an interactive SWF piece.
			var quiz:MediaElement = mediaFactory.createMediaElement( new URLResource( QUIZ_SWF ) );
			
			//Marker 5: Create a event listener for "correct" bubbled up from the SWF if response is correct
			this.addEventListener( "correct", _onCorrect );
			
			//Marker 6: Construct the SerialElement adding each media element in the desired order
			var serialElement:SerialElement = new SerialElement();
			serialElement.addChild( preRoll );
			serialElement.addChild( element );
			serialElement.addChild( quiz );
			
			//Marker 7: Create a LayoutMetaData object to set the unified display parameters for the serial composition and apply it to the metadata
			var layoutData:LayoutMetadata = new LayoutMetadata();
			layoutData.scaleMode = ScaleMode.LETTERBOX;
			layoutData.width = 800;
			layoutData.height = 600;
			
			serialElement.metadata.addValue( LayoutMetadata.LAYOUT_NAMESPACE, layoutData );
			
			//the simplified api controller for media
			player = new MediaPlayer( serialElement );
			
			//the container (sprite) for managing display and layout
			container = new MediaContainer();
			container.addMediaElement( serialElement );
			
			
			//Adds the container to the stage
			this.addChild( container );
		}
		
		
		protected function _onCorrect( p_evt:Event ):void
		{
			trace("CORRECT RECEIVED");
			
			//Marker 8: Note the below - and that if correct answer was triggered full play is accessed
			var element:MediaElement = mediaFactory.createMediaElement( new StreamingURLResource( DYNAMIC_STREAMING, StreamType.RECORDED, 15 )  );
			player.media = element;
			container.addMediaElement( element );
			
		}
		
	}
}